package com.kitchen.service.impl;

import com.kitchen.common.vo.ResultVO;
import com.kitchen.common.vo.ResStatus;
import com.kitchen.beans.entity.ClassroomCategory;
import com.kitchen.beans.vo.ClassroomCategoryVo;
import com.kitchen.mapper.ClassroomCategoryMapper;
import com.kitchen.service.ClassroomCategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 78468
 * 课堂分类接口实现
 */
@Service
public class ClassroomCategoryServiceImpl implements ClassroomCategoryService {

    @Autowired
    private ClassroomCategoryMapper classroomCategoryMapper;

    @Transactional(propagation = Propagation.SUPPORTS,rollbackFor = Exception.class)
    @Override
    public ResultVO showAll() {
        ResultVO resultVO = new ResultVO();
        try {
            /**
             * 一级分类
             */
            Example example = new Example(ClassroomCategory.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("ccLevel",1);
            List<ClassroomCategory> classroomCategories = classroomCategoryMapper.selectByExample(example);
            List<ClassroomCategoryVo> transform = transform(classroomCategories);

            /**
             * 二级分类
             */
            for (ClassroomCategoryVo classroomCategoryVo : transform) {
                Example example1 = new Example(ClassroomCategory.class);
                Example.Criteria criteria1 = example1.createCriteria();
                criteria1.andEqualTo("ccBelongCategory",classroomCategoryVo.getCcId());
                List<ClassroomCategory> classroomCategories1 = classroomCategoryMapper.selectByExample(example1);
                List<ClassroomCategoryVo> transform1 = transform(classroomCategories1);

                classroomCategoryVo.setCategoryVos(transform1);
            }

            resultVO.setCode(ResStatus.SUCCESS);
            resultVO.setMsg(ResStatus.QUERY_ALL_CATEGORIES_SUCCESSFULLY);
            resultVO.setData(transform);
        }catch (Exception e){
            resultVO.setCode(ResStatus.FAIL);
            resultVO.setMsg(ResStatus.FAILED_TO_QUERY_ALL_CATEGORIES);
        }
        return resultVO;
    }


    /**
     * 将
     * @param list
     * @return
     */
    public static List<ClassroomCategoryVo> transform (List<ClassroomCategory> list){
        ArrayList<ClassroomCategoryVo> classroomCategoryVos = new ArrayList<>();
        for (ClassroomCategory classroomCategory : list) {
            ClassroomCategoryVo classroomCategoryVo = new ClassroomCategoryVo();
            BeanUtils.copyProperties(classroomCategory,classroomCategoryVo);
            classroomCategoryVos.add(classroomCategoryVo);
        }
        return classroomCategoryVos;
    }
}
